package com.hyyt.shopjsp.phone.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.phone.pojo.CoinMallProduct;
import com.hyyt.shopjsp.phone.pojo.CoinMallProductType;
import com.hyyt.shopjsp.phone.service.ICoinMallProductService;
import com.hyyt.shopjsp.phone.service.ICoinMallProductTypeService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SHOP_积分商城分类表 Controller
 *  LC
 */
@Controller
@RequestMapping("/back/coinMallProductType")
public class CoinMallProductTypeController  extends BaseController {

    private Logger log = Logger.getLogger(PhoneHomeKeyBookController.class);
    private String basePath = "/back/phone/coinMallProductType/";

    @Resource
    private ICoinMallProductTypeService coinMallProductTypeService;//积分商城分类service接口
    @Resource
    private ICoinMallProductService coinMallProductService;//积分商城分类service接口

    /**
     * 进入积分商城分类
     *
     * @param param
     * @return
     */
    @RequestMapping("gotoCoinMallProductType")
    public String gotoCoinMallProductType(Map<String, Object> param) {
        param.put("typeId", "0");
        return basePath + "coinMallProductTypeList";
    }

    /**
     * 查询所有积分商城分类
     *
     * @param page
     * @param response
     * @param typeName  分类名称
     */
    @RequestMapping(value = "listCoinMallProductType", method = RequestMethod.POST)
    @ResponseBody
    public void listCoinMallProductType(Page page,String typeName,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(typeName)) {
            param.put("typeName", typeName.trim());
        }
        param.put("typeParentId", "0");
        param.put("sortColumns", "sort asc");
        int totalRecordCount = coinMallProductTypeService.countCoinMallProductType(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<CoinMallProductType> coinMallProductTypeList = coinMallProductTypeService.queryCoinMallProductType(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", coinMallProductTypeList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据积分商城分类id获得对象
     *
     * @param typeId
     */
    @RequestMapping(value = "getCoinMallProductTypeInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getCoinMallProductTypeInfo(Integer typeId) {
        if (Utils.objectIsNotEmpty(typeId)) {
            CoinMallProductType coinMallProductType = coinMallProductTypeService.loadCoinMallProductType(typeId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(coinMallProductType));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改积分商城分类
     *
     * @param coinMallProductType
     * @return
     */
    @RequestMapping(value = "saveOrUpdateCoinMallProductType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateCoinMallProductType(CoinMallProductType coinMallProductType) {
        if (Utils.objectIsNotEmpty(coinMallProductType)) {
            if (Utils.objectIsEmpty(coinMallProductType.getTypeId())) {
                coinMallProductTypeService.saveCoinMallProductType(coinMallProductType);
            } else {
                coinMallProductTypeService.updateCoinMallProductType(coinMallProductType);
            }
            Map<String,Object> params = new HashMap<>();
            params.put("idName","0");
            params.put("operateType","navigate");
            params.put("success","true");
            return ResultMsg.create(true,null,FastJsonUtils.toJSONString(params));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 删除传入的积分商城分类id
     *
     * @param typeId
     */
    @RequestMapping(value = "deleteCoinMallProductType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteCoinMallProductType(Integer typeId) {
        if (Utils.objectIsNotEmpty(typeId)) {
            coinMallProductTypeService.deleteCoinMallProductType(typeId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id展示左侧积分商城树形结构
     * @param response
     */
    @RequestMapping("getNodes")
    public void getNodes(HttpServletResponse response){
        List<CoinMallProductType> coinMallProductTypes =  coinMallProductTypeService.queryAllCoinMallProductType();
        List nodeList = new ArrayList();
        HashMap nodeMap = new HashMap();
        for(int i = 0; i < coinMallProductTypes.size(); i++) {
            CoinMallProductType cpt = coinMallProductTypes.get(i);
            nodeMap = new HashMap();
            nodeMap.put("id",cpt.getTypeId());
            nodeMap.put("text", cpt.getTypeName());
            nodeMap.put("isLeaf", cpt.getLoadType());
            String parentId = String.valueOf(cpt.getTypeParentId());
            if (Utils.objectIsEmpty(parentId)) {
                parentId = "0";
            }
            nodeMap.put("parentId",parentId);
            nodeList.add(nodeMap);
        }
        //获取分类的json字符串信息
        String jsonInfo= NodeUtil.getNodeJson(nodeList,"积分商城分类");
        ResultMsg.create(jsonInfo, response, false);
    }

    /**
     * 查询所有积分商城商品
     *
     * @param page
     * @param response
     * @param productName  商品名称
     */
    @RequestMapping(value = "listCoinMallProductInfoByTypeId", method = RequestMethod.POST)
    @ResponseBody
    public void listCoinMallProductInfoByTypeId(Page page,String typeId,String productName,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(productName)) {
            param.put("productName", productName.trim());
        }
        if(Utils.objectIsNotEmpty(typeId)){
            param.put("typeId", typeId);
        }
        param.put("sortColumns", "id desc");
        int totalRecordCount = coinMallProductService.countCoinMallProduct(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<CoinMallProduct> coinMallProductList = coinMallProductService.queryCoinMallProduct(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", coinMallProductList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

}
